﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;

using System.Windows.Forms;
using Utility;

namespace hpm
{
    class clsBenhNhan
    {
        string _maBenhNhan, _hoTen, _gioiTinh, _maNghe, _maDanToc;

        DAL da = new DAL();
        public static DataTable dtbBenhNhan;
        SqlCommand slqcom;
        public static SqlDataAdapter sqlDataAdapter = null;
        SqlCommandBuilder bui = null;

        public string MaBenhNhan
        {
            get; set;
        }
        
        public string NgayTao
        {
            get; set;
        }

        public string NgaySua
        {
            get; set;
        }
         
        public string HoTen
        {
            get; set;
        }

        public string NgaySinh
        {
            get; set;
        }


        public string GioiTinh
        {
            get; set;
        }

        public string MaNghe
        {
            get; set;
        }

        public string MaDanToc
        {
            get; set;
        }

        public string MaDoiTuong
        { get; set; }


        public string DienThoai
        {
            get; set;
        }

        public string MaQuocTich
        {
            get; set;
        }

        public string DiaChi
        {
            get; set;
        }
        public string MaTinh
        {
            get; set;
        }

        public string MaHuyen
        {
            get; set;
        }

        public string NoiLamViec
        {
            get; set;
        }

        public string Email
        {
            get; set;
        }

        public string NguonGioiThieu
        {
            get; set;
        }
        public string MaThe
        { 
            get; set; 
        }

        public string CMND
        {
            get; set;
        }

        public Byte[] HinhAnh { get; set; }
        public string NT_HoTen { get; set; }
        public string NT_DienThoai { get; set; }
        public string NT_QuanHe { get; set; }
        public string NT_DiaChi { get; set; }





        public clsBenhNhan()
        {
            _maBenhNhan = _hoTen = _gioiTinh = _maNghe = _maDanToc = "";
        }



        public string getMaxId()
        {
            da.ketnoiServer();
            this.slqcom = new SqlCommand();
            this.slqcom.CommandText = "SELECT MaBenhNhan FROM BenhNhan WHERE NgayTao = (select max(NgayTao) from BenhNhan)";

            this.slqcom.Connection = da.con;
            SqlDataReader reader = slqcom.ExecuteReader();
            reader.Read();
            string MaBenhNhan = reader["MaBenhNhan"].ToString();
            return MaBenhNhan;
        }



        public DataTable getAllBenhNhan()
        {
            da.ketnoiServer();
            dtbBenhNhan = new DataTable();
            this.slqcom = new SqlCommand();
            this.slqcom.CommandText = "Select MaBenhNhan, NgayTao, HoTen, GioiTinh, DiaChi, DienThoai from BenhNhan order by NgayTao desc";
            this.slqcom.Connection = da.con;
            this.slqcom.CommandType = CommandType.Text;
            
            //this.sqlDataAdapter = new SqlDataAdapter(this.slqcom);
            //this.sqlDataAdapter.Fill(dtbBenhNhan);

            //da.da_closeConnect();
            return dtbBenhNhan;
        }


        public DataTable getAllBenhNhan1()
        {
            da.ketnoiServer();
            dtbBenhNhan = new DataTable();
            this.slqcom = new SqlCommand();
            this.slqcom.CommandText = "Select MaBenhNhan, NgayTao, HoTen, GioiTinh, DiaChi, DienThoai from BenhNhan order by NgayTao desc";
            this.slqcom.Connection = da.con;
            this.slqcom.CommandType = CommandType.Text;

            //this.sqlDataAdapter = new SqlDataAdapter(this.slqcom);
            //this.sqlDataAdapter.Fill(dtbBenhNhan);



           // sqlDataAdapter = new SqlDataAdapter("Select MaBenhNhan, NgayTao, HoTen, HinhAnh, GioiTinh, DiaChi, DienThoai, MaQuocTich from BenhNhan order by NgayTao desc", da.connString);
            sqlDataAdapter = new SqlDataAdapter("Select * from DsBenhNhan order by NgayTao desc", da.connString);

            bui = new SqlCommandBuilder(sqlDataAdapter);
            dtbBenhNhan = new DataTable();
            sqlDataAdapter.Fill(dtbBenhNhan);

            da.da_closeConnect();
            return dtbBenhNhan;
        }


        public clsBenhNhan GetBenhNhanById()
        {
            da.ketnoiServer();
            this.slqcom = new SqlCommand();
            //this.slqcom.CommandText = "Select * from BenhNhan WHERE MaBenhNhan = '7854723' ";

            this.slqcom.CommandText = "Select * from DsBenhNhan WHERE MaBenhNhan = '"+ this.MaBenhNhan +"' ";

            this.slqcom.Connection = da.con;
            SqlDataReader reader = slqcom.ExecuteReader();
            reader.Read();
            
            string[] ignoreFields = { "HinhAnh", "MaNghe", "MaDanToc", "MaQuocTich", "MaTinh", "MaHuyen", "MaDoiTuong" };
            foreach (PropertyInfo prop in typeof(clsBenhNhan).GetProperties())
            {
                if (ignoreFields.Contains(prop.Name))
                    ;
                else
                    prop.SetValue(this, reader[prop.Name].ToString(), null);


                switch (prop.Name)
                {

                    case "MaNghe":
                        prop.SetValue(this, reader["TenNghe"].ToString(), null);
                        break;

                    case "MaQuocTich":
                        prop.SetValue(this, reader["TenQuocTich"].ToString(), null);
                        break;

                    case "MaTinh":
                        prop.SetValue(this, reader["TenTinh"].ToString(), null);
                        break;

                    case "MaHuyen":
                        prop.SetValue(this, reader["TenQuan"].ToString(), null);
                        break;

                    case "MaDoiTuong":
                        prop.SetValue(this, reader["TenDoiTuong"].ToString(), null);
                        break;

                    case "MaDanToc":
                        prop.SetValue(this, reader["DanToc"].ToString(), null);
                        break;

                    case "HinhAnh":
                        if (reader["HinhAnh"].ToString() != "")
                            prop.SetValue(this, (Byte[])reader["HinhAnh"], null);
                        else
                            prop.SetValue(this, new Byte[0], null);
                        break;
                }
            }
            
            da.da_closeConnect();
            return this;
        }


        public void saveBenhNhan(string maBenhNhan = "")
        {
            da.ketnoiServer();

            var sqlQuery = "";
            if (maBenhNhan == "")
                sqlQuery = "select * from BenhNhan where 0 = 1";
            else
                sqlQuery = "select * from BenhNhan where MaBenhNhan = '" + maBenhNhan + "'";
            
            sqlDataAdapter = new SqlDataAdapter(sqlQuery, da.connString);
            bui = new SqlCommandBuilder(sqlDataAdapter);
            dtbBenhNhan = new DataTable();
            sqlDataAdapter.Fill(dtbBenhNhan);

            if (maBenhNhan == "")
            {
                DataRow dr;
                dr = dtbBenhNhan.NewRow();
                this.buidDataRow(dr);
                dtbBenhNhan.Rows.Add(dr);
            }
            else
            {
                this.buildDataTableBeforeUpdate(dtbBenhNhan);
            }
            sqlDataAdapter.Update(dtbBenhNhan);
            da.da_closeConnect();
        }


        private void buildDataTableBeforeUpdate(DataTable dtTableObj)
        {
            foreach (PropertyInfo prop in typeof(clsBenhNhan).GetProperties())
            {

                if (prop.GetValue(this, null) == null)
                    ;
                else
                    dtTableObj.Rows[0][prop.Name] = prop.GetValue(this, null);
            }
        }


        private DataRow buidDataRow(DataRow dr)
        {
            string[] ignoreFields = { "NgaySua" };

            foreach (PropertyInfo prop in typeof(clsBenhNhan).GetProperties())
            {
                if (ignoreFields.Contains(prop.Name))
                {}
                else
                    dr[prop.Name] = prop.GetValue(this, null);
            }
            return dr;
        }


        public bool CheckExist(string maBenhNhan)
        {
            da.ketnoiServer();
            this.slqcom = new SqlCommand();
            this.slqcom.CommandText = "SELECT * FROM BenhNhan WHERE MaBenhNhan = '" + maBenhNhan + "'";

            this.slqcom.Connection = da.con;
            SqlDataReader reader = slqcom.ExecuteReader();
            //reader.Read();

            //MessageBox.Show(reader.HasRows.ToString());

            return reader.Read();
        }



        public void test()
        {
            this.MaBenhNhan = "BuiXuanThuan";
            foreach (PropertyInfo prop in typeof(clsBenhNhan).GetProperties())
            {
                //dr[prop.Name] = prop.Name;

                MessageBox.Show(prop.Name + " - " + prop.GetValue(this, null));
            }
        }




        /*
        public static void AddBenhNhan()
        {
            DataRow dr;
            dr = dtbBenhNhan.NewRow();
            dr["MaBenhNhan"] = "789456123";
            dr["HoTen"] = "Trịnh Xuân Thuận";

            dtbBenhNhan.Rows.Add(dr);
            sqlDataAdapter.Update(dtbBenhNhan);
        }
         */
    }
}
